package com.lagou.controller;


import com.lagou.service.ILagouAuthCodeService;
import com.lagou.tool.CommonResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.juli.logging.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaohuzi
 * @since 2022-03-26
 */
@RestController
@RequestMapping("/lagou-token")
@Slf4j
public class LagouTokenController {

    @Autowired
    private ILagouAuthCodeService lagouAuthCodeServiceImpl;



    @RequestMapping("/saveToken/{token}/{email}")
    public CommonResponse saveToken(@PathVariable("token")String token, @PathVariable("email")String email){
        if(lagouAuthCodeServiceImpl.addUserToken(token,email)>0){

        return CommonResponse.success();
        };

        return CommonResponse.Fail();
    }

    @RequestMapping("/getEmail/{token}")
    public String getEmailByToken(@PathVariable("token") String token){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String consumerHeader = request.getHeader("token");
        if(consumerHeader!=null && !"".equals(consumerHeader)){
            try {
                log.info("----1,token is:{}" , URLDecoder.decode(consumerHeader,"UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }else{
            log.info("----2,token is:{}" +  consumerHeader);
        }

        return lagouAuthCodeServiceImpl.getEmailByToken(token);
    }
}
